package com.jz.community.moduleshopping.shopCart.adapter;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.basecomm.routerUtils.RouterCommonUtils;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.shopCart.bean.CartShopInfo;
import com.jz.community.moduleshopping.shopCart.presenter.ShopCartPresenter;

import java.util.List;

/**
 * 购物车店铺
 */
public class ShopCartAdapter extends BaseQuickAdapter<CartShopInfo, BaseViewHolder> {

    private ShopCartPresenter mShopCartPresenter;
    private RecyclerView cartValidListRv;
    private ValidCartItemAdapter validCartItemAdapter;
    private CheckShopItemListener checkShopItemListener;
    private CheckBox cartShopCheck;

    public void setCheckShopItemListener(CheckShopItemListener checkShopItemListener) {
        this.checkShopItemListener = checkShopItemListener;
    }

    public ShopCartAdapter(@Nullable List<CartShopInfo> data, ShopCartPresenter shopCartPresenter) {
        super(R.layout.module_shopping_item_cart_valid_shop_layout, data);
        mShopCartPresenter = shopCartPresenter;
    }

    @Override
    protected void convert(BaseViewHolder helper, CartShopInfo item) {
        showShopName(helper, item.getName());
        handleBindCartValidItemAdapter(item, helper);
    }

    private void showShopName(BaseViewHolder helper, String shopName) {
        TextView cartShopName = helper.getView(R.id.cart_shop_name);
        cartValidListRv = helper.getView(R.id.cart_list_rv);
        cartShopName.setText(shopName);
        changeCheck(helper, helper.getAdapterPosition());
    }

    private void changeCheck(BaseViewHolder helper, int position) {
        cartShopCheck = helper.getView(R.id.cart_shop_check);
        cartShopCheck.setChecked("Y".equalsIgnoreCase(getItem(position).getChecked()) ? true : false);

        cartShopCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mShopCartPresenter.updateShopAllGoodsItemChk("Y".equalsIgnoreCase(getItem(position).getChecked()) ? false : true, position, new OnLoadListener<List<CartShopInfo>>() {
                    @Override
                    public void onSuccess(List<CartShopInfo> cartShopInfos) {
                        checkShopItemListener.isCheckShopItem(cartShopInfos, position);
                    }

                    @Override
                    public void onFail(String msg, int code) {

                    }
                });

            }
        });
    }

    private void handleBindCartValidItemAdapter(CartShopInfo shopInfosBean, BaseViewHolder helper) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        cartValidListRv.setLayoutManager(layoutManager);
        validCartItemAdapter = new ValidCartItemAdapter(shopInfosBean.getCartInfos());
        cartValidListRv.setAdapter(validCartItemAdapter);

        validCartItemAdapter.setCheckItemListener(new ValidCartItemAdapter.CheckItemListener() {
            @Override
            public void isCheckItem(int position, boolean isChecked) {
                mShopCartPresenter.updateGoodsItemChk(position, isChecked, helper.getAdapterPosition(), new OnLoadListener<List<CartShopInfo>>() {
                    @Override
                    public void onSuccess(List<CartShopInfo> cartShopInfos) {
                        checkShopItemListener.isCheckShopItem(cartShopInfos, position);
                    }

                    @Override
                    public void onFail(String msg, int code) {

                    }
                });
            }

            @Override
            public void isUpdateCheckItemNum(int position, int num) {
                mShopCartPresenter.changeRecycleViewItemNum(position, num, helper.getAdapterPosition());
                mShopCartPresenter.updateCartShopBuyNum();
            }
        });
        validCartItemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startToCartTypeActivity(ConverterUtils.toInt(shopInfosBean.getCartInfos().get(position).getCartType()), position, shopInfosBean);
            }
        });
    }

    /**
     * 不同类型跳转
     *
     * @param cartType
     * @param position
     */
    private void startToCartTypeActivity(int cartType, int position, CartShopInfo shopInfosBean) {
        switch (cartType) {
            case 1:
                ARouter.getInstance().build(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL)
                        .withString("url", shopInfosBean.getCartInfos().get(position).getGsLink())
                        .navigation();
                break;
            case 2:
                Bundle bundle = new Bundle();
                bundle.putString("shopId", shopInfosBean.getId() + "");
                RouterCommonUtils.startParamsActivity(RouterIntentConstant.NEAR_SHOP, bundle);
                break;
        }
    }

    public interface CheckShopItemListener {
        void isCheckShopItem(List<CartShopInfo> cartShopInfoList, int position);
    }
}
